<!DOCTYPE html>
<html  lang="zh-CN" >
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="theme-color" content="#fff" id="theme-color">
  <meta name="description" content="无情">
  <link rel="icon" href="/img/hxf233333.png">
  <title>MySQL学习笔记_day5</title>
  
  
  <meta property="og:title" content="MySQL学习笔记_day5">
  
  
  <meta property="og:url" content="https://hxf233333.gitee.io/2021/02/28/MySQL%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0_day5/index.html">
  
  
  <meta property="og:img" content="/img/hxf233333.png">
  
  
  
  <meta property="og:type" content="article">
  <meta property="og:article:published_time" content="2021-02-28">
  <meta property="og:article:modified_time" content="2021-02-28">
  <meta property="og:article:author" content="hxf233333">
  
  
  <meta property="og:article:tag" content="MySQL">
  
  
  
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
    var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
      }
    };
    setDarkmode();
  </script>
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
  </script>
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
  <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
  <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  <link rel="prefetch" href="//cdn.jsdelivr.net/npm/mathjax@3.0.5/es5/tex-svg.js" as="script">
  
  
  
  <link rel="prefetch" href="https://unpkg.com/valine/dist/Valine.min.js" as="script">
  
  
  
  
<link rel="stylesheet" href="/css/main.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">

  
  
<link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">

  
  
<link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">

  
  
  
  
  
  
  <link href="/js/lib/prism/prism.min.css" rel="stylesheet" data-prism="prism">
  
  
  
<link rel="stylesheet" href="/js/lib/prism/prism-line-numbers.min.css">

  
  
  
<meta name="generator" content="Hexo 5.3.0"><link rel="alternate" href="/atom.xml" title="无情" type="application/atom+xml">
</head>


<body>
  <div class="wrapper">
    
    <nav class="navbar">
  <div class="navbar-logo">
    <span class="navbar-logo-main">
      
      <img class="navbar-logo-img" src="/img/hxf233333.png" alt="logo">
      
      <span class="navbar-logo-dsc">hxf233333</span>
    </span>
  </div>
  <div class="navbar-menu">
    
    <a href="/" class="navbar-menu-item">
    
    首页
    
    </a>
    
    <a href="/archives" class="navbar-menu-item">
    
    归档
    
    </a>
    
    <a href="/tags" class="navbar-menu-item">
    
    标签
    
    </a>
    
    <a href="/categories" class="navbar-menu-item">
    
    分类
    
    </a>
    
    <a href="/about" class="navbar-menu-item">
    
    关于
    
    </a>
    
    <a href="/links" class="navbar-menu-item">
    
    友链
    
    </a>
    
    <a class="navbar-menu-item darknavbar" id="dark"><i class="iconfont icon-weather"></i></a>
    <a class="navbar-menu-item searchnavbar" id="search"><i class="iconfont icon-search" style="font-size: 1.2rem; font-weight: 400;"></i></a>
  </div>
</nav>
    
    <div id="local-search" style="display: none;">
      <input class="navbar-menu-item" id="search-input" placeholder="请输入搜索内容...">
      <div id="search-content"></div>
    </div>
    
    <div class="section-wrap">
      <div class="container">
        <div class="columns">
          <main class="main-column">
<article class="card card-content">
  <header>
    <h1 class="post-title">
      MySQL学习笔记_day5
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2021-02-28T11:01:34.000Z">
      <i class="iconfont icon-calendar" style="margin-right: 2px;"></i>
      <span>2021-02-28</span>
    </time>
    
    <span class="dot"></span>
    
    <a href="/categories/学习笔记/" class="post-meta-link">学习笔记</a>
    
    
    
    <span class="dot"></span>
    <span>1.6k 字</span>
    
  </div>
  
  <div class="post-meta post-show-meta" style="margin-top: -10px;">
    <div style="display: flex; align-items: center;">
      <i class="iconfont icon-biaoqian" style="margin-right: 2px; font-size: 1.15rem;"></i>
      
      
        <a href="/tags/MySQL/" class="post-meta-link">MySQL</a>
      
    </div>
  </div>
  
  </header>
  <div id="section" class="post-content">
    <h3 id="5-7-子查询"><a href="#5-7-子查询" class="headerlink" title="5.7 子查询"></a>5.7 子查询</h3><ul>
<li><p>说明：</p>
<p>当一个查询语句中又嵌套了另一个完整的select语句，则被嵌套的select语句称为子查询或内查询，外面的select语句称为主查询或外查询。</p>
</li>
<li><p>分类：</p>
<p>按子查询出现的位置进行分类：</p>
</li>
</ul>
<blockquote>
<p>1、select后面<br>    要求：子查询的结果为单行单列（标量子查询）<br>2、from后面<br>    要求：子查询的结果可以为多行多列<br>3、where或having后面 ★<br>    要求：子查询的结果必须为单列<br>        单行子查询<br>        多行子查询<br>4、exists后面<br>    要求：子查询结果必须为单列（相关子查询）</p>
</blockquote>
<ul>
<li>特点：</li>
</ul>
<div class="post-note note-danger"><p>1、子查询放在条件中，要求必须放在条件的右侧<br>2、子查询一般放在小括号中<br>3、子查询的执行优先于主查询<br>4、单行子查询对应了 单行操作符：&gt; &lt; &gt;= &lt;= = &lt;&gt;<br>5、多行子查询对应了 多行操作符：any/some  all in </p></div>

<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">#一、放在where或having后面</span>
<span class="token comment">#一）单行子查询</span>

<span class="token comment">#案例1：谁的工资比 Abel 高?</span>
<span class="token comment">#①查询Abel的工资</span>
<span class="token keyword">SELECT</span> salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> last_name  <span class="token operator">=</span> <span class="token string">'Abel'</span><span class="token punctuation">;</span>
<span class="token comment">#②查询salary>①的员工信息</span>
<span class="token keyword">SELECT</span> last_name<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> salary<span class="token operator">></span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> salary
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> last_name <span class="token operator">=</span> <span class="token string">'Abel'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#案例2：返回job_id与141号员工相同，salary比143号员工多的员工姓名，job_id 和工资</span>
<span class="token comment">#①查询141号员工的job_id</span>
<span class="token keyword">SELECT</span> job_id
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> employee_id <span class="token operator">=</span> <span class="token number">141</span>

<span class="token comment">#②查询143号员工的salary</span>
<span class="token keyword">SELECT</span> salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> employee_id <span class="token operator">=</span> <span class="token number">143</span>

<span class="token comment">#③查询job_id=① and salary>②的信息</span>
<span class="token keyword">SELECT</span> last_name<span class="token punctuation">,</span>job_id<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> job_id
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> employee_id <span class="token operator">=</span> <span class="token number">141</span>
<span class="token punctuation">)</span> <span class="token operator">AND</span> salary<span class="token operator">></span><span class="token punctuation">(</span>

	<span class="token keyword">SELECT</span> salary
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> employee_id <span class="token operator">=</span> <span class="token number">143</span>

<span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token comment">#案例3：返回公司工资最少的员工的last_name,job_id和salary</span>
<span class="token comment">#①查询最低工资</span>
<span class="token keyword">SELECT</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> employees
<span class="token comment">#②查询salary=①的员工的last_name,job_id和salary</span>
<span class="token keyword">SELECT</span> last_name<span class="token punctuation">,</span>job_id<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> salary<span class="token operator">=</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span>
	<span class="token keyword">FROM</span> employees

<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#案例4：查询最低工资大于50号部门最低工资的部门id和其最低工资</span>
<span class="token comment">#①查询50号部门的最低工资</span>
<span class="token keyword">SELECT</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span>
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> department_id <span class="token operator">=</span> <span class="token number">50</span><span class="token punctuation">;</span>

<span class="token comment">#②查询各部门的最低工资，筛选看哪个部门的最低工资>①</span>
<span class="token keyword">SELECT</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span><span class="token punctuation">,</span>department_id
<span class="token keyword">FROM</span> employees
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> department_id
<span class="token keyword">HAVING</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span><span class="token operator">></span><span class="token punctuation">(</span>

	<span class="token keyword">SELECT</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span>
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> department_id <span class="token operator">=</span> <span class="token number">50</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token comment">#二）多行子查询</span>
<span class="token comment">/*

in:判断某字段是否在指定列表内  
x in(10,30,50)

any/some:判断某字段的值是否满足其中任意一个

x>any(10,30,50)
x>min()

x=any(10,30,50)
x in(10,30,50)

all:判断某字段的值是否满足里面所有的

x >all(10,30,50)
x >max()

*/</span>


<span class="token comment">#案例1：返回location_id是1400或1700的部门中的所有员工姓名</span>
<span class="token comment">#①查询location_id是1400或1700的部门</span>
<span class="token keyword">SELECT</span> department_id
<span class="token keyword">FROM</span> departments
<span class="token keyword">WHERE</span> location_id <span class="token operator">IN</span><span class="token punctuation">(</span><span class="token number">1400</span><span class="token punctuation">,</span><span class="token number">1700</span><span class="token punctuation">)</span>

<span class="token comment">#②查询department_id = ①的姓名</span>
<span class="token keyword">SELECT</span> last_name
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> department_id <span class="token operator">IN</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> department_id
	<span class="token keyword">FROM</span> departments
	<span class="token keyword">WHERE</span> location_id <span class="token operator">IN</span><span class="token punctuation">(</span><span class="token number">1400</span><span class="token punctuation">,</span><span class="token number">1700</span><span class="token punctuation">)</span>

<span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token comment">#题目：返回其它部门中比job_id为‘IT_PROG’部门任一工资低的员工的员工号、姓名、job_id 以及salary</span>
<span class="token comment">#①查询job_id为‘IT_PROG’部门的工资</span>
<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token string">'IT_PROG'</span>

<span class="token comment">#②查询其他部门的工资&lt;任意一个①的结果</span>
<span class="token keyword">SELECT</span> employee_id<span class="token punctuation">,</span>last_name<span class="token punctuation">,</span>job_id<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> salary<span class="token operator">&lt;</span><span class="token keyword">ANY</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> salary
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token string">'IT_PROG'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

等价于

<span class="token keyword">SELECT</span> employee_id<span class="token punctuation">,</span>last_name<span class="token punctuation">,</span>job_id<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> salary<span class="token operator">&lt;</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token function">MAX</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span>
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token string">'IT_PROG'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">#案例3：返回其它部门中比job_id为‘IT_PROG’部门所有工资都低的员工 的员工号、姓名、job_id 以及salary</span>
<span class="token comment">#①查询job_id为‘IT_PROG’部门的工资</span>
<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token string">'IT_PROG'</span>

<span class="token comment">#②查询其他部门的工资&lt;所有①的结果</span>
<span class="token keyword">SELECT</span> employee_id<span class="token punctuation">,</span>last_name<span class="token punctuation">,</span>job_id<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> salary<span class="token operator">&lt;</span><span class="token keyword">ALL</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token keyword">DISTINCT</span> salary
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token string">'IT_PROG'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>

等价于

<span class="token keyword">SELECT</span> employee_id<span class="token punctuation">,</span>last_name<span class="token punctuation">,</span>job_id<span class="token punctuation">,</span>salary
<span class="token keyword">FROM</span> employees
<span class="token keyword">WHERE</span> salary<span class="token operator">&lt;</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token function">MIN</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span>
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> job_id <span class="token operator">=</span> <span class="token string">'IT_PROG'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token comment">#二、放在select后面</span>
<span class="token comment">#案例；查询部门编号是50的员工个数</span>
<span class="token keyword">SELECT</span> 
<span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token function">COUNT</span><span class="token punctuation">(</span><span class="token operator">*</span><span class="token punctuation">)</span>
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> department_id <span class="token operator">=</span> <span class="token number">50</span>
<span class="token punctuation">)</span>   个数<span class="token punctuation">;</span>


<span class="token comment">#三、放在from后面</span>

<span class="token comment">#案例：查询每个部门的平均工资的工资级别</span>
<span class="token comment">#①查询每个部门的平均工资</span>
<span class="token keyword">SELECT</span> <span class="token function">AVG</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span><span class="token punctuation">,</span>department_id
<span class="token keyword">FROM</span> employees
<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> department_id

<span class="token comment">#②将①和sal_grade两表连接查询</span>
<span class="token keyword">SELECT</span> dep_ag<span class="token punctuation">.</span>department_id<span class="token punctuation">,</span>dep_ag<span class="token punctuation">.</span>ag<span class="token punctuation">,</span>g<span class="token punctuation">.</span>grade
<span class="token keyword">FROM</span> sal_grade g
<span class="token keyword">JOIN</span> <span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token function">AVG</span><span class="token punctuation">(</span>salary<span class="token punctuation">)</span> ag<span class="token punctuation">,</span>department_id
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">GROUP</span> <span class="token keyword">BY</span> department_id
<span class="token punctuation">)</span> dep_ag <span class="token keyword">ON</span> dep_ag<span class="token punctuation">.</span>ag <span class="token operator">BETWEEN</span> g<span class="token punctuation">.</span>min_salary <span class="token operator">AND</span> g<span class="token punctuation">.</span>max_salary<span class="token punctuation">;</span>


<span class="token comment">#四、放在exists后面</span>
<span class="token comment">#案例1 ：查询有无名字叫“张三丰”的员工信息</span>
<span class="token keyword">SELECT</span> <span class="token keyword">EXISTS</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> <span class="token operator">*</span> 
	<span class="token keyword">FROM</span> employees
	<span class="token keyword">WHERE</span> last_name <span class="token operator">=</span> <span class="token string">'Abel'</span>

<span class="token punctuation">)</span> 有无Abel<span class="token punctuation">;</span>


<span class="token comment">#案例2：查询没有女朋友的男神信息</span>
<span class="token keyword">USE</span> girls<span class="token punctuation">;</span>
<span class="token keyword">SELECT</span> bo<span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">FROM</span> boys bo
<span class="token keyword">WHERE</span> bo<span class="token punctuation">.</span><span class="token punctuation">`</span>id<span class="token punctuation">`</span> <span class="token operator">NOT</span> <span class="token operator">IN</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> boyfriend_id
	<span class="token keyword">FROM</span> beauty b
<span class="token punctuation">)</span>

<span class="token keyword">SELECT</span> bo<span class="token punctuation">.</span><span class="token operator">*</span>
<span class="token keyword">FROM</span> boys bo
<span class="token keyword">WHERE</span> <span class="token operator">NOT</span> <span class="token keyword">EXISTS</span><span class="token punctuation">(</span>
	<span class="token keyword">SELECT</span> boyfriend_id
	<span class="token keyword">FROM</span> beauty b
	<span class="token keyword">WHERE</span> bo<span class="token punctuation">.</span>id <span class="token operator">=</span> b<span class="token punctuation">.</span>boyfriend_id
<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="5-8-分页查询"><a href="#5-8-分页查询" class="headerlink" title="5.8 分页查询"></a>5.8 分页查询</h3><ul>
<li>应用场景：当页面上的数据，一页显示不全，则需要分页显示</li>
</ul>
<p><code>分页查询的sql命令请求数据库服务器——&gt;服务器响应查询到的多条数据——&gt;前台页面</code></p>
<ul>
<li>语法：</li>
</ul>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> 查询列表
<span class="token keyword">from</span> 表<span class="token number">1</span> 别名
<span class="token keyword">join</span> 表<span class="token number">2</span> 别名
<span class="token keyword">on</span> 连接条件
<span class="token keyword">where</span> 筛选条件
<span class="token keyword">group</span> <span class="token keyword">by</span> 分组
<span class="token keyword">having</span> 分组后筛选
<span class="token keyword">order</span> <span class="token keyword">by</span> 排序列表
<span class="token keyword">limit</span> 起始条目索引<span class="token punctuation">,</span>显示的条目数<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>执行顺序：</li>
</ul>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token number">1</span>、<span class="token keyword">from</span>子句
<span class="token number">2</span>、<span class="token keyword">join</span>子句
<span class="token number">3</span>、<span class="token keyword">on</span>子句
<span class="token number">4</span>、<span class="token keyword">where</span>子句
<span class="token number">5</span>、<span class="token keyword">group</span> <span class="token keyword">by</span>子句
<span class="token number">6</span>、<span class="token keyword">having</span>子句
<span class="token number">7</span>、<span class="token keyword">select</span>子句
<span class="token number">8</span>、<span class="token keyword">order</span> <span class="token keyword">by</span>子句
<span class="token number">9</span>、<span class="token keyword">limit</span>子句<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>特点：</li>
</ul>
<div class="post-note note-danger"><p>①起始条目索引如果不写，默认是0<br>②limit后面支持两个参数<br>参数1：显示的起始条目索引<br>参数2：条目数</p></div>

<ul>
<li>公式：</li>
</ul>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">假如要显示的页数是page，每页显示的条目数为size

<span class="token keyword">select</span> <span class="token operator">*</span>
<span class="token keyword">from</span> employees
<span class="token keyword">limit</span> <span class="token punctuation">(</span>page<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">*</span>size<span class="token punctuation">,</span>size<span class="token punctuation">;</span>

page		size<span class="token operator">=</span><span class="token number">10</span>
<span class="token number">1</span>           <span class="token keyword">limit</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token number">10</span>
<span class="token number">2</span>			<span class="token keyword">limit</span> <span class="token number">10</span><span class="token punctuation">,</span><span class="token number">10</span>
<span class="token number">3</span>			<span class="token keyword">limit</span> <span class="token number">20</span><span class="token punctuation">,</span><span class="token number">10</span>
<span class="token number">4</span>			<span class="token keyword">limit</span> <span class="token number">30</span><span class="token punctuation">,</span><span class="token number">10</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="5-9-联合查询"><a href="#5-9-联合查询" class="headerlink" title="5.9 联合查询"></a>5.9 联合查询</h3><ul>
<li><p>说明：</p>
<p><code>当查询结果来自于多张表，但多张表之间没有关联，这个时候往往使用联合查询，也称为union查询</code></p>
</li>
</ul>
<ul>
<li>语法：</li>
</ul>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">select</span> 查询列表 <span class="token keyword">from</span> 表<span class="token number">1</span>  <span class="token keyword">where</span> 筛选条件  
<span class="token keyword">union</span>
<span class="token keyword">select</span> 查询列表 <span class="token keyword">from</span> 表<span class="token number">2</span>  <span class="token keyword">where</span> 筛选条件  <span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<ul>
<li>特点：<br>1、多条待联合的查询语句的查询列数必须一致，查询类型、字段意义最好一致<br>2、union实现去重查询;union all 实现全部查询，包含重复项</li>
</ul>
<pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">#案例：查询所有国家的年龄>20岁的用户信息</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> usa <span class="token keyword">WHERE</span> uage <span class="token operator">></span><span class="token number">20</span> <span class="token keyword">UNION</span>
<span class="token keyword">SELECT</span> <span class="token operator">*</span> <span class="token keyword">FROM</span> chinese <span class="token keyword">WHERE</span> age <span class="token operator">></span><span class="token number">20</span> <span class="token punctuation">;</span>

<span class="token comment">#案例2：查询所有国家的用户姓名和年龄</span>
<span class="token keyword">SELECT</span> uname<span class="token punctuation">,</span>uage <span class="token keyword">FROM</span> usa
<span class="token keyword">UNION</span>
<span class="token keyword">SELECT</span> age<span class="token punctuation">,</span><span class="token punctuation">`</span>name<span class="token punctuation">`</span> <span class="token keyword">FROM</span> chinese<span class="token punctuation">;</span>

<span class="token comment">#案例3：union自动去重/union all 可以支持重复项</span>
<span class="token keyword">SELECT</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'范冰冰'</span> 
<span class="token keyword">UNION</span> <span class="token keyword">ALL</span>
<span class="token keyword">SELECT</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'范冰冰'</span> 
<span class="token keyword">UNION</span>  <span class="token keyword">ALL</span>
<span class="token keyword">SELECT</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'范冰冰'</span> 
<span class="token keyword">UNION</span>  <span class="token keyword">ALL</span>
<span class="token keyword">SELECT</span> <span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'范冰冰'</span> <span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

  </div>
  <div>
  
  <div class="post-note note-warning copyright" style="margin-top: 42px">
    <p><span style="font-weight: bold;">作者：</span><a target="_blank" rel="nofollow noopener noreferrer" href="https://hxf233333.gitee.io/about">hxf233333</a></p>
    <p><span style="font-weight: bold;">文章链接：</span><a target="_blank" rel="nofollow noopener noreferrer" href="https://hxf233333.gitee.io/2021/02/28/MySQL%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0_day5/">https://hxf233333.gitee.io/2021/02/28/MySQL%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0_day5/</a></p>
    <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
  </div>
  
  </div>
</article>
<div class="nav">
  
  <div class="nav-item-prev">
    <a href="/2021/03/01/MySQL学习笔记_day6/" class="nav-link">
      <i class="iconfont icon-left nav-prev-icon"></i>
      <div>
        <div class="nav-label">上一篇</div>
        
        <div class="nav-title">MySQL学习笔记_day6 </div>
        
      </div>
    </a>
  </div>
  
  
  <div class="nav-item-next">
    <a href="/2021/02/27/MySQL学习笔记_day4/" class="nav-link">
      <div>
        <div class="nav-label">下一篇</div>
        
        <div class="nav-title">MySQL学习笔记_day4 </div>
        
      </div>
      <i class="iconfont icon-right nav-next-icon"></i>
    </a>
  </div>
  
</div>

<div class="card card-content comment-card" style="margin-top: 16px;">
  <div class="comment-card-title">评论</div>
  
  <div id="vcomments"></div>
  
  <script>
    loadScript("https://unpkg.com/valine/dist/Valine.min.js");
    var oldLoadVa = window.onload;
    window.onload = function () {
      oldLoadVa && oldLoadVa();
      new Valine({
        el: '#vcomments',
        appId: 'TILGM6Ga3CyeOjwCwggjbzaD-MdYXbMMI',
        appKey: 's6cTuJch2xdAViWNL3P7uRx2',
        placeholder: 'Just go go',
        path: window.location.pathname,
        avatar: 'mp',
        meta: ["nick","mail","link"],
        pageSize: '10',
        lang: '',
        visitor: 'false',
        highlight: true,
        recordIP: false,
        
        
        
        enableQQ: 'false',
        requiredFields: [],
      });
    };
  </script>

</div>

<div class="card card-content toc-card" id="mobiletoc">
  <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-7-%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.7 子查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-8-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.8 分页查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-9-%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.9 联合查询</span></a></li></ol>
</div></main>
          <aside class="left-column">
            
            <div class="card card-author">
              
<img src="/img/hxf233333.png" class="author-img">

<p class="author-name">hxf233333</p>
<p class="author-description">make a dream</p>
<div class="author-message">
  <a class="author-posts-count" href="/archives">
    <span>15</span>
    <span>文章</span>
  </a>
  <a class="author-categories-count" href="/categories">
    <span>5</span>
    <span>分类</span>
  </a>
  <a class="author-tags-count" href="/tags">
    <span>3</span>
    <span>标签</span>
  </a>
</div>

            </div>
            
            <div class="sticky-tablet">
  
  
  <article class="display-when-two-columns spacer">
    <div class="card card-content toc-card">
      <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-7-%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.7 子查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-8-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.8 分页查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-9-%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.9 联合查询</span></a></li></ol>
    </div>
  </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header"><i class="iconfont icon-fenlei" style="padding-right: 2px;"></i>分类</div>
  <div class="categories-list">
    
      <a href="/categories/学习笔记">
        <div class="categories-list-item">
          学习笔记
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/常用工具">
        <div class="categories-list-item">
          常用工具
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/思维导图">
        <div class="categories-list-item">
          思维导图
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/搭建环境">
        <div class="categories-list-item">
          搭建环境
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
      <a href="/categories/教程">
        <div class="categories-list-item">
          教程
          <span class="categories-list-item-badge">1</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header"><i class="iconfont icon-biaoqian" style="padding-right: 2px;"></i>热门标签</div>
  <div class="tags-list">
    
    <a href="\tags\MySQL" title="MySQL"><div class="tags-list-item">MySQL</div></a>
    
    <a href="\tags\Linux" title="Linux"><div class="tags-list-item">Linux</div></a>
    
    <a href="\tags\教程" title="教程"><div class="tags-list-item">教程</div></a>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
          <aside class="right-column">
            <div class="sticky-widescreen">
  
  
  <article class="card card-content toc-card">
    <div class="toc-header"><i class="iconfont icon-menu" style="padding-right: 2px;"></i>目录</div>
<ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#5-7-%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.7 子查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-8-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.8 分页查询</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-9-%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-text">5.9 联合查询</span></a></li></ol>
  </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header"><i class="iconfont icon-wenzhang_huaban" style="padding-right: 2px;"></i>最近文章</div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-04-04</div>
        <a href="/2021/04/04/git版本控制/"><div class="recent-posts-item-content">git版本控制</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-06</div>
        <a href="/2021/03/06/MySQL学习笔记_day10(完结)/"><div class="recent-posts-item-content">MySQL学习笔记_day10(完结)</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-05</div>
        <a href="/2021/03/05/MySQL学习笔记_day9/"><div class="recent-posts-item-content">MySQL学习笔记_day9</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2021-03-03</div>
        <a href="/2021/03/03/MySQL学习笔记_day8/"><div class="recent-posts-item-content">MySQL学习笔记_day8</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
          </aside>
        </div>
      </div>
    </div>
  </div>
  
  <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>Copyright ©
          
          2020 -
          
          2021
        </span>
        &nbsp;
        <a href="/" class="footer-link">hxf233333 </a>
      </div>
    </div>

    
    
    
    
      <div class="footer-dsc">
        
        本站总访问量<span id="busuanzi_value_site_pv"></span>次
        
        
        <span>&nbsp;|&nbsp;</span>
        
        
        本站总访客数<span id="busuanzi_value_site_uv"></span>次
        
      </div>
      
    
</footer>
  <a role="button" id="scrollbutton" class="basebutton"  aria-label="回到顶部">
  <i class="iconfont icon-arrowleft button-icon"></i>
</a>
<a role="button" id="menubutton" class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a role="button" id="popbutton" class="basebutton" aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a role="button" id="darkbutton" class="basebutton darkwidget" aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a role="button" id="searchbutton" class="basebutton searchwidget" aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a>

  
  
  

  
  
  <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img');
    var i;
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a');
      wrapper.setAttribute('href', img[i].getAttribute('data-src'));
      wrapper.setAttribute('aria-label', 'illustration');
      wrapper.style.cssText = 'width: 100%; display: flex; justify-content: center;';
      if (img[i].alt) wrapper.dataset.caption = img[i].alt;
      wrapper.dataset.nolink = true;
      img[i].before(wrapper);
      wrapper.append(img[i]);
      var divWrap = document.createElement('div');
      divWrap.classList.add('gallery');
      wrapper.before(divWrap);
      divWrap.append(wrapper);
    }
    baguetteBox.run('.gallery');
  }
</script>
<script>loadScript("/js/lib/lightbox/baguetteBox.min.js", addImgLayout)</script>
  
  
  
<script src="/js/main.js"></script>

  
  <script>loadScript("/js/lib/busuanzi.min.js")</script>
  
  
  <script>
    var addLazyload = function () {
      var observer = lozad('.lozad', {
        load: function (el) {
          el.srcset = el.getAttribute('data-src');
        },
        loaded: function (el) {
          el.classList.add('loaded');
        }
      });
      observer.observe();
    }
  </script>
  <script>loadScript("/js/lib/lozad.min.js", addLazyload)</script>
  
  
</body>

</html>